package com.cloudeasy.user.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloudeasy.api.util.EntityUtil;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.user.RoleVO;
import com.cloudeasy.api.vo.user.UserRoleVO;
import com.cloudeasy.user.field.UserRoleField;
import com.cloudeasy.user.po.Role;
import com.cloudeasy.user.po.UserRole;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户角色工具类
 */
public class UserRoleUtil {
    /**
     * 构建
     *
     * @param userIds        用户Id列表
     * @param roleIds        角色Id类别
     * @param operateAreas 操作区域列表
     * @param withSelf       是否可以操作本区域
     * @param statuses       状态列表
     * @param asc            正序排序字段
     * @param desc           倒序排序字段
     * @return {@link QueryWrapper<UserRole>}
     */
    public static QueryWrapper<UserRole> compose(List<String> userIds, List<String> roleIds,
                                                 List<String> operateAreas, Boolean withSelf,
                                                 List<Integer> statuses, String[] asc, String[] desc) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        if (userIds != null && userIds.size() > 0) {
            queryWrapper.in("user_id", userIds);
        }
        if (roleIds != null && roleIds.size() > 0) {
            queryWrapper.in("role_id", roleIds);
        }
        if (operateAreas != null && operateAreas.size() > 0) {
            operateAreas.forEach(operateAreaId -> queryWrapper.or(wrapper -> wrapper.eq("operate_area", operateAreaId)));
        }
        if (withSelf != null) {
            queryWrapper.eq("with_self", withSelf);
        }
        if (statuses != null && statuses.size() > 0) {
            queryWrapper.in("status", statuses);
        }
        if (asc != null && asc.length > 0) {
            EntityUtil.checkParamter(UserRoleField.getFieldList(), asc);
            queryWrapper.orderByAsc(asc);
        }
        if (desc != null && desc.length > 0) {
            EntityUtil.checkParamter(UserRoleField.getFieldList(), desc);
            queryWrapper.orderByDesc(desc);
        }
        return queryWrapper;
    }

    /**
     * 转换VO对象
     *
     * @param userRole {@link UserRole}
     * @param role     {@link Role}
     * @return {@link UserRoleVO}
     */
    public static UserRoleVO convert(UserRole userRole, Role role) {
        UserRoleVO userRoleVO = new UserRoleVO();
        BeanUtils.copyProperties(userRole, userRoleVO);
        RoleVO roleVO = new RoleVO();
        BeanUtils.copyProperties(role, roleVO);
        userRoleVO.setRole(roleVO);
        return userRoleVO;
    }

    /**
     * 转换VO对象
     *
     * @param userRoles {@link List<UserRole>}
     * @param roles     {@link List<Role>}
     * @return {@link List<UserRoleVO>}
     */
    public static List<UserRoleVO> convert(List<UserRole> userRoles, List<Role> roles) {
        Map<String, Role> roleMap = roles.stream().collect(Collectors.toMap(Role::getId, role -> role, (o, o2) -> o));
        return userRoles.stream().map(userRole -> convert(userRole, roleMap.get(userRole.getRoleId()))).collect(Collectors.toList());
    }

    /**
     * 转换VO对象
     *
     * @param userRoleIPage {@link IPage<UserRole>}
     * @param roles         {@link List<Role>}
     * @return {@link PageResult<UserRoleVO>}
     */
    public static PageResult<UserRoleVO> convert(IPage<UserRole> userRoleIPage, List<Role> roles) {
        Map<String, Role> roleMap = roles.stream().collect(Collectors.toMap(Role::getId, role -> role, (o, o2) -> o));
        PageResult<UserRoleVO> userRoleVOPageResult = new PageResult<>();
        userRoleVOPageResult.setPages(userRoleIPage.getPages())
                .setTotal(userRoleIPage.getTotal())
                .setRecords(userRoleIPage.convert(userRole -> convert(userRole, roleMap.get(userRole.getRoleId()))).getRecords());
        return userRoleVOPageResult;
    }
}
